package com.skytech.component.crawler.scratch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PageContentService {
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Autowired
    private TargetRulesDAO targetRulesDAO;
    @Value("${elasticsearch.server}")
    private String server;


    public JSONObject getData(ResponseEntity<JSONObject> responseEntity) {
        JSONArray jsonArray = responseEntity.getBody().getJSONObject("hits").getJSONArray("hits");
        String total = responseEntity.getBody().getJSONObject("hits").get("total").toString();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (Object js : jsonArray) {
            JSONObject object = JSONObject.parseObject(js.toString());
            JSONObject jsonObject1 = object.getJSONObject("_source");

            //jsonObject1.put("time",UTCStringtODefaultString(object.getJSONObject("_source").get("@timestamp").toString()));
            jsonObjectList.add(jsonObject1);
        }

        JSONObject jsonObjectNew = new JSONObject();
        jsonObjectNew.put("data", jsonObjectList);
        jsonObjectNew.put("total", total);


        return jsonObjectNew;
    }

    public JSONObject listDataCountByTypeAndTime(JSONObject jsonObject) {

      //  JSONObject json=preResolveCondition(jsonObject);
        return getDataCount(preResolveCondition(jsonObject));

    }

    private JSONObject preResolveCondition(JSONObject jsonObject) {
        String dataType = jsonObject.getString("data_type");

        String startTime = jsonObject.getString("start_time");
        String endTime = jsonObject.getString("end_time");
        //List<TargetRules> idList = targetRulesDAO.findAllByDataType(dataType);
        String query;

        query = "{\n" +
                "            \"query\": {\n" +
                "                \"bool\": {\n" +
                "                   \n" +
                "                    \"must\":{\n" +
                "                    \n" +
                "                      \"match\":\"\"\n" +
                "                    },\n" +
                "                    \"filter\":{\n" +
                "                      \"range\": \"\"\n" +
                "                    } \n" +
                "                }\n" +
                "\n" +
                "            },\n" +
                "\n" +
                "            \"size\": \"\",\n" +
                "            \"from\": \"\",\n" +
                "        }";
        JSONObject json = JSON.parseObject(query);

        JSONObject  mustJSON=new JSONObject();
        mustJSON.put("dataType",dataType);
        json.getJSONObject("query").getJSONObject("bool").getJSONObject("must").put("match",mustJSON);
        JSONObject filter = new JSONObject();
        if (!StringUtils.isEmpty(startTime)) {
            filter.put("gt", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            filter.put("lt", endTime);
        }
        if (!filter.isEmpty()) {
            JSONObject timestamp = new JSONObject();
            timestamp.put("timestamp", filter);
            JSONObject range = new JSONObject();
            range.put("range", timestamp);
            json.getJSONObject("query").getJSONObject("bool").put("filter",range);
            //json.getJSONObject("query").put("filter",range);
            //mustJSONArray.add(range);
        }
        int size=10;
        if(null!=jsonObject.get("size")) {
            size = jsonObject.getInteger("size");
        }

        json.put("size", size);

        int from=1;
        if(null!=jsonObject.get("from")) {
            from = jsonObject.getInteger("from");
        }

        json.put("from", (from - 1) * size);
        //json.put("data_type",dataType);


        return json;

    }

    private JSONObject getDataCount(JSONObject jsonObject){
        String dataType=jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("must").getJSONObject("match").getString("dataType");
        //jsonObject.remove("data_type");
        //ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "crawler_news/news/_search", jsonObject, JSONObject.class);
        ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + dataType+"/news/_search", jsonObject, JSONObject.class);

        String total = responseEntity.getBody().getJSONObject("hits").get("total").toString();


        JSONObject jsonObjectNew = new JSONObject();

        jsonObjectNew.put("data_count", total);
        //jsonObjectNew.put("data_type",dataType);
        jsonObjectNew.put("start_time",jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("filter").getJSONObject("range").getJSONObject("timestamp").getString("gt"));
        jsonObjectNew.put("end_time",jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("filter").getJSONObject("range").getJSONObject("timestamp").getString("lt"));


        return jsonObjectNew;
    }

    public JSONObject listDataByTypeAndTime(JSONObject jsonObject) {
        return getDataDetails(preResolveCondition(jsonObject));
    }

    private JSONObject getDataDetails(JSONObject jsonObject) {
        String dataType=jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("must").getJSONObject("match").getString("dataType");
      //  jsonObject.remove("data_type");
     //   ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "crawler_news/news/_search", jsonObject, JSONObject.class);
        ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + dataType+"/news/_search", jsonObject, JSONObject.class);

        JSONArray jsonArray = responseEntity.getBody().getJSONObject("hits").getJSONArray("hits");
        //String total = responseEntity.getBody().getJSONObject("hits").get("total").toString();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (Object js : jsonArray) {
            JSONObject object = JSONObject.parseObject(js.toString());
            JSONObject jsonObject1 = object.getJSONObject("_source");
          /*  if(!object.getJSONObject("_source").getString("publish_time").isEmpty()){
                //object
                String time=object.getJSONObject("_source").getString("publish_time");
            }*/
            //jsonObject1.put("time",UTCStringtODefaultString(object.getJSONObject("_source").get("@timestamp").toString()));
            jsonObjectList.add(jsonObject1);
        }

        JSONObject jsonObjectNew = new JSONObject();
        jsonObjectNew.put("data_type",jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("must").getJSONObject("match").getString("dataType"));
        jsonObjectNew.put("data", jsonObjectList);
        jsonObjectNew.put("start_time",jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("filter").getJSONObject("range").getJSONObject("timestamp").getString("gt"));
        jsonObjectNew.put("end_time",jsonObject.getJSONObject("query").getJSONObject("bool").getJSONObject("filter").getJSONObject("range").getJSONObject("timestamp").getString("lt"));
        jsonObjectNew.put("from",jsonObject.getInteger("from")/jsonObject.getInteger("size")+1);
        jsonObjectNew.put("size",jsonObject.getInteger("size"));
        return jsonObjectNew;
    }

}

